//
//  SGSearchBar.m
//  Labanbrowser
//
//  Created by Simon Grätzer on 10.08.12.
//
//
//  Copyright (c) 2012 Simon Peter Grätzer
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//
#import "SGSearchField.h"
#include "SGTabDefines.h"
#import "MyUtilities.h"
@implementation SGSearchField{
    UIImage* favouriteOnImage;
    UIImage* favouriteOffImage;
}
@synthesize state = _state;
-(void)relocalize{
    self.placeholder = [MyUtilities getLocalizeStringFromTag:@"AddressPlaceHolder"];
}

- (id)initWithDelegate:(id<UITextFieldDelegate>)delegate {
    if (self = [super initWithFrame:CGRectMake(0, 0, 280., 30.)]) {
        self.delegate = delegate;
        self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        self.placeholder = [MyUtilities getLocalizeStringFromTag:@"AddressPlaceHolder"];
        self.keyboardType = UIKeyboardTypeASCIICapable;
        self.autocapitalizationType = UITextAutocapitalizationTypeNone;
        self.autocorrectionType = UITextAutocorrectionTypeNo;
        self.borderStyle = UITextBorderStyleRoundedRect;
        self.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.textColor = [UIColor darkTextColor];
        
        
        CGRect btnRect = CGRectMake(0, 0, 22, 22);
        _favouriteItem = [UIButton buttonWithType:UIButtonTypeCustom];
        self.favouriteItem.frame = btnRect;
        self.favouriteItem.backgroundColor = [UIColor clearColor];
        favouriteOnImage=[UIImage imageNamed:@"ic_browser_titlebar_add_bookmark_full.png"];
        favouriteOffImage=[UIImage imageNamed:@"ic_browser_titlebar_add_bookmark_nor.png"];
        [self.favouriteItem setImage:favouriteOffImage forState:UIControlStateNormal];
        [self.favouriteItem setImage:[UIImage imageNamed:@"ic_browser_titlebar_add_bookmark_hl.png"] forState:UIControlStateHighlighted];
        [self.favouriteItem setImage:[UIImage imageNamed:@"ic_browser_titlebar_add_bookmark_dis.png"] forState:UIControlStateDisabled];
        
        self.favouriteItem.adjustsImageWhenDisabled=NO;
        self.favouriteItem.adjustsImageWhenHighlighted=NO;
        
        
        self.leftView = self.favouriteItem;
        self.leftViewMode = UITextFieldViewModeAlways;//UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone ?
        //UITextFieldViewModeUnlessEditing : UITextFieldViewModeAlways;
        self.rightViewMode = UITextFieldViewModeUnlessEditing;
        
        _reloadItem = [UIButton buttonWithType:UIButtonTypeCustom];
        self.reloadItem.frame = btnRect;
        self.reloadItem.backgroundColor = [UIColor clearColor];
        self.reloadItem.showsTouchWhenHighlighted = YES;
        [self.reloadItem setImage:[UIImage imageNamed:@"reload"] forState:UIControlStateNormal];
        
        _stopItem = [UIButton buttonWithType:UIButtonTypeCustom];
        self.stopItem.frame = btnRect;
        self.stopItem.backgroundColor = [UIColor clearColor];
        self.stopItem.showsTouchWhenHighlighted = YES;
        [self.stopItem setImage:[UIImage imageNamed:@"stop"] forState:UIControlStateNormal];
        
        self.state = SGSearchFieldStateDisabled;
        
        self.inputAccessoryView = [self generateInputAccessoryView];
    }
    return self;
}



- (void)drawTextInRect:(CGRect)rect {
    if (self.editing)
        [super drawTextInRect:rect];
    else {
        NSString *text = [self.text stringByReplacingOccurrencesOfString:@"http://" withString:@""];
        text = [text stringByReplacingOccurrencesOfString:@"https://" withString:@""];
        [text drawInRect:rect withFont:self.font
           lineBreakMode:NSLineBreakByTruncatingTail
               alignment:self.textAlignment];
    }
}

- (UIView *)generateInputAccessoryView {
    UIToolbar *_inputAccessory = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.superview.bounds.size.width, 44.)];
    _inputAccessory.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        _inputAccessory.translucent = YES;
        _inputAccessory.tintColor = kTabColor;
    }
    
    UIBarButtonItem *btn, *flex, *fix;
    flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                         target:nil action:nil];
    fix = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                                        target:nil action:nil];
    fix.width = 10;
    
    NSArray *titles = @[@":", @".", @"-", @"/", @".com"];
    NSMutableArray *buttons = [NSMutableArray arrayWithCapacity:titles.count];
    [buttons addObject:flex];
    for (NSString *title in titles) {
        btn = [[UIBarButtonItem alloc] initWithTitle:title
                                               style:UIBarButtonItemStyleBordered
                                              target:self
                                              action:@selector(addText:)];
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
            btn.tintColor = [UIColor lightGrayColor];
        
        btn.width = 40.;
        [buttons addObject:btn];
        [buttons addObject:fix];
    }
    [buttons addObject:flex];
    _inputAccessory.items = buttons;
    return _inputAccessory;
}

- (IBAction)addText:(UIBarButtonItem *)sender {
    [self insertText:sender.title];
}

- (void)setState:(SGSearchFieldState)state {
    if (_state == state)
        return;
    
    _state = state;
    switch (state) {
        case SGSearchFieldStateDisabled:
            self.reloadItem.enabled = NO;
            self.rightView = self.reloadItem;
            break;
            
        case SGSearchFieldStateReload:
            self.reloadItem.enabled = YES;
            self.rightView = self.reloadItem;
            break;
            
        case SGSearchFieldStateStop:
            self.rightView = self.stopItem;
            break;
    }
}

-(void)changeImageBasedOnFoundBookmark:(id)bookmark{
    if(bookmark!=nil){
        [self.favouriteItem setImage:favouriteOnImage forState:UIControlStateNormal];
    }else{
        [self.favouriteItem setImage:favouriteOffImage forState:UIControlStateNormal];
    }
}

@end
